//
//  SendPatrolViewController.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/11/13.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "SendPatrolViewController.h"
#import "PatrolAddModel.h"
#import "MsgObjTableViewCell.h"
#import "SendMsgTableViewCell.h"
#import "SendMsgContentTableViewCell.h"
#import "UITextView+Placeholder.h"
#import "MsgFootView.h"
#import "ChooseView.h"
#import "RegisterShopModel.h"
#import "ChooseMoreViewController.h"
@interface SendPatrolViewController ()<UITableViewDelegate,UITableViewDataSource,UITextViewDelegate>

@property(nonatomic,strong)TB_TableView *homeTableView;

@property(nonatomic,strong)PatrolAddModel *PModel;

@property(nonatomic,strong)NSMutableArray*MsgobjcArr;

@end

@implementation SendPatrolViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title=@"检查日志";
    
    self.PModel=[PatrolAddModel new];
    
    self.PModel.staffId=[NSString stringWithFormat:@"%ld",[LoginStatuesModel getStaffId]];
    
     self.PModel.shopId=self.Postmodel.shopId;
    
     self.PModel.ptype=self.Postmodel.ptype;
    
    self.PModel.shopName=self.shopName;
    
    if (self.Postmodel.shopId==0) {
        
        self.PModel.isChooseShopid=NO;

    }else{
        
        self.PModel.isChooseShopid=YES;
  
        
    }
    
    [self.view addSubview:self.homeTableView];
    
    
    for (NSInteger i=0; i<2; i++) {
        
        RSItems *items=[RSItems new];
        
        if (i==0) {
          
            items.name=@"商户";
            
            items.value=@"1";
            
        }else{
            items.name=@"开办方";
        
            items.value=@"2";
        }
        [self.MsgobjcArr addObject:items];
        
        
    }
    
    // Do any additional setup after loading the view.
}

-(NSMutableArray*)MsgobjcArr
{
    if (!_MsgobjcArr) {
        
        _MsgobjcArr=[NSMutableArray array];
        
    }
    
    return _MsgobjcArr;
}

-(TB_TableView*)homeTableView
{
    if (!_homeTableView) {
        
        _homeTableView=[[TB_TableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
        
        _homeTableView.delegate=self;
        
        _homeTableView.dataSource=self;
        
        _homeTableView.tableFooterView=[[UIView alloc]init];
        
         [_homeTableView registerNib:[UINib nibWithNibName:@"MsgObjTableViewCell" bundle:nil] forCellReuseIdentifier:@"ObjcCell"];
        
        [_homeTableView registerNib:[UINib nibWithNibName:@"SendMsgTableViewCell" bundle:nil] forCellReuseIdentifier:@"Receivecell"];
        
         [_homeTableView registerNib:[UINib nibWithNibName:@"SendMsgContentTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell2"];
        
         [_homeTableView registerClass:[MsgFootView class] forHeaderFooterViewReuseIdentifier:@"Foot"];
        
    }
    
    return _homeTableView;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.PModel.isChooseShopid) {
        
        return 1;
    }
    
    return 3;
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.PModel.isChooseShopid){
        
        SendMsgContentTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        
        cell.msgTextView.placeholder=@"请输入详细内容";
        
           cell.msgTextView.delegate=self;
        
        return cell;
        
        
    }else{
        
        if (indexPath.row==0) {
            
            
            MsgObjTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ObjcCell" forIndexPath:indexPath];
            
            if ([self.Postmodel.ptype isEqualToString:@"1"]) {
                
                cell.PostObjLabel.text=@"商户";
        
                
            }else{
                
                cell.PostObjLabel.text=@"开办方";
             
            }
      
            return cell;
            
        }else if (indexPath.row==1) {
            
            SendMsgTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Receivecell" forIndexPath:indexPath];
   
            cell.NumLable.text=self.PModel.shopName;
            
            cell.block=^(NSInteger type)
            {
                
                
                [self ChoosePostMan];
                
            };
            
            return cell;
        }else{
            
            SendMsgContentTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
            
            cell.msgTextView.placeholder=@"请输入详细内容";
            
             cell.msgTextView.delegate=self;
            
            return cell;
            
            
        }
        
        
    }

}


-(void)ChoosePostMan
{
    //除了平台外，不需要列表
    
    ChooseMoreViewController *choose=[ChooseMoreViewController new];
    
    JobListModel *ReqModel=[JobListModel new];
    
    choose.isSingeChoose=YES;//单选
    
    if ([self.PModel.ptype isEqualToString:@"1"]) {
        
           choose.title=@"选择发送商户";
        
            choose.DataReqStr=@"/index.php/FoodCheckApi/shopList.json";
        
            choose.Reqmodel=ReqModel;
        
            choose.listType=0;
        
    }else{
        
            choose.title=@"选择发送开办方";
        
            choose.DataReqStr=@"/index.php/FoodCheckApi/marketList.json";
        
            choose.Reqmodel=ReqModel;
        
            choose.listType=2;
        
    }

    choose.singleBlock=^(NSInteger  shopid, NSString *name)
    {
        self.PModel.shopName=name;
        
        self.PModel.shopId=shopid;
    
        [self.homeTableView reloadData];
        
    };
    
    
    [self.navigationController pushViewController:choose animated:YES];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     if (self.PModel.isChooseShopid){
         
         return 150;
         
     }else{
         
         if (indexPath.row==0||indexPath.row==1) {
             
             return 50;
             
         }else{
             
             return 150;
             
         }
         
         
     }

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 90;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}


-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    MsgFootView *foot=[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"Foot"];
    
    [foot.MainBtn addTarget:self action:@selector(SendPatrol) forControlEvents:UIControlEventTouchUpInside];
    
    return foot;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
     if (self.PModel.isChooseShopid){
         
         
         
     }else{
    
    if (indexPath.row==0) {
        
        //跳转，商户或开办方列表
        
        [self ChooseObj];
        
        }else if (indexPath.row==1)
        {
         
        [self ChoosePostMan];
   
        }
    
     }
}


-(void)ChooseObj
{
    ChooseView *chooseView=[[ChooseView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kDevice_Is_iPhoneX?SCREEN_HEIGHT-64-IPhoneXTableHeight:SCREEN_HEIGHT-64) andDataArr:self.MsgobjcArr andBlock:^(NSString *value, NSInteger index) {
        
        MsgObjTableViewCell *cell=[self.homeTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        
        RSItems *items=self.MsgobjcArr[index];
        
        cell.PostObjLabel.text=items.name;
        
        self.PModel.ptype=value;
        
    }];
    
    [chooseView PopWithView:self.view];
    
}


-(void)SendPatrol
{
  
    if (self.PModel.shopId==0) {
        
        [MBProgressHUD showWarnMessage:@"发送对象"];
        
        return ;
        
    }
    
    if (self.PModel.content.length==0) {
        
         [MBProgressHUD showWarnMessage:@"发送内容不能为空"];
        
        return;
    }
    
    
    [self showLoadingAnimation];
    
    BaseReqApi *api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/patrolAdd.json" andrequestTime:5 andParams:[self.PModel mj_keyValuesWithIgnoredKeys:@[@"isChooseShopid",@"shopName"]] andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        [self stopLoadingAnimation];
        
        if (responseStatus==1) {
            
            [MBProgressHUD showSuccessMessage:@"新增成功"];
            
            _block();
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }else if (responseStatus==0)
        {
            
            [MBProgressHUD showErrorMessage:message];
            
        }else if (responseStatus==2)
        {
            [MBProgressHUD showErrorMessage:message];
            
        }else{
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
            
        }
    }];
    
    
    
}

- (void)textViewDidChange:(UITextView *)textView
{
    
    self.PModel.content=textView.text;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
